<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model\LazyCommon;
use App\Base\BaseModel;
use App\Entity\PlatConfig;
use App\Setting\LazyCommon\PlatConfigSetting;

class PlatConfigModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new PlatConfigSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    public function getRep()
    {
        return  $this->getDoctrine()->getRepository("App:PlatConfig");
    }



    /****************************************** 私有 *****************************************/

    /**
     * @param $catId
     * @param int $limit
     * @return PlatConfig[]
     */
    public function getByCategory($catId,$limit = 6)
    {
        return $this->getRep()->findBy(array("category"=> $catId),array("orders"=> "ASC"),$limit,0);
    }

    /**
     * @param $id
     * @return PlatConfig
     */
    public function get($id)
    {
        return $this->getRep()->find($id);
    }
}